#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/2/28 18:15
@desc
"""
from ippool.lib.pooldao.oper_db import upsert, truncate
from ippool.lib.pooldao.ip_pool_dao import IpPool


class WriteTable:

    @staticmethod
    def rewrite_db_from_file(file_name):
        with open(file_name, 'r') as f:
            n = 1
            for i in f:
                print(n, i.split())
                n += 1
                line = i.split()
                ippoolobj = IpPool(ip_port=line[0], protocol=line[1], get_time=line[2])
                upsert(ippoolobj)
                # print(ippoolobj)

    @staticmethod
    def analysis_deal_file(file_name):
        dist = {}
        with open(file_name, 'r') as f:
            n = 1
            for i in f:
                data = i.split()
                # print(n, data)
                dist[data[0]] = dist[data[0]] + 1 if data[0] in dist else 1
                n += 1
            # print(dist)
            print(len(dist))

    @staticmethod
    def truncate_ip_table():
        truncate()


if __name__ == '__main__':
    WriteTable.truncate_ip_table()
    wt = WriteTable()
    wt.rewrite_db_from_file(r"D:\iProject\myPython\com\teradata\ippool\get_check\proxy_list_20190227111955.txt")
    # WriteTable.analysis_deal_file(r"D:\iProject\myPython\com\teradata\ippool\get_check\proxy_list_20190227111955.txt")
